//@flow


/**
 * 权限列表配置
 */


import React, { Component } from 'react';
import { 
    View,
    Text,
    Platform,
 } from 'react-native';
import { 
    List,
    Switch,
    Toast,
 } from "antd-mobile-rn";
import { PageHOC } from '../base/pageHOC';
import { NavBar } from '../container/navBar';
import { PermissionList,PermissionProps } from '../util/permission/PermissionManager';
import Permissions from "react-native-permissions";

type State = {
    permissionStatus  : {[key:string]:boolean}
}

// @PageHOC
export class PermissionsControl extends Component <{},State>{

    /**初始化 */
    constructor(props){
        super(props)
        this.permissions = PermissionList.filter(item=>(item.platform=='all'||item.platform==Platform.OS));
        let obj :{[key:string]:boolean} = {};
        this.permissions.map(item=>{
            obj[item.key] = false
            return item;
        })
        this.state = {
            permissionStatus:obj
        }
    }
    // /**初始化state */
    // state:State = {
    //     permissionStatus:{}
    // }
    
    /**初始化，获取权限初始状态 */
    componentDidMount(){
        this.getStatus()
    }
    /**获取初始状态 */
    getStatus(){
        Permissions.checkMultiple(this.permissions.map(item=>item.key)).then(res=>{
            this.setState({
                permissionStatus:Object.keys(res).map(item=>{return{[item]:res[item]=='authorized'} })
            })
        })
    }
    /**渲染每一行 */
    renderItem(item:PermissionProps){
        return (
            <List.Item key={item.label} extra={<Switch checked={this.state.permissionStatus[item.key]} 
                                      onChange={(value)=>{
                                        this.authChange(item,value)
                                      }}/>}>
                {item.label}
            </List.Item>
        )
    }
    /**authorized change */
    authChange(item:PermissionProps,toValue:boolean){
        Permissions.request(item.key,item.type||null).then(res=>{
            if(res == 'authorized'){
                //用户同意授权
                this.changeValue(item,toValue)
            }else {
                /**
                 * 其他情况
                 * 1，用户已经禁止授权 - 只能去setting设置
                 * 2，app还未请求授权 - 这种情况 不会发生，只有check调用才有
                 */
                Permissions.canOpenSettings().then(canOpen=>{
                    if(canOpen){
                        Permissions.openSettings()
                    }else{
                        Toast.fail('打开设置中心失败，请手动前往设置',3,null,false)
                    }
                })
            }
        })
    }
    /**修改当前界面值 */
    changeValue(item:PermissionProps,toValue:boolean){
        let newObj : {[key:string]:boolean} = {...this.state.permissionStatus};
        newObj[item.key] = toValue;
        this.setState({permissionStatus:{...newObj}})
    }
    render(){
        return (
           <View>
                <NavBar titleView="权限控制"/>
                <List>
                   {
                       this.permissions.map(item=>{
                           return this.renderItem(item)
                       })
                   }
                </List>
           </View>
        )
    }
}